package com.chen.cachetest.controller;

import com.chen.cachetest.entity.User;
import com.chen.cachetest.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;

    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("/findUser/{name}")
    public User getUserByName(@PathVariable String name){

        if (redisTemplate.hasKey(name)){
            System.out.println("redis存在缓存");
            return (User) redisTemplate.opsForValue().get(name);
        }

        System.out.println("redis不存在缓存");
        redisTemplate.opsForValue().set(name,userService.findUserBuName(name));
        return userService.findUserBuName(name);
    }
}
